var express = require('express');
var securityRoutes = express.Router();
var jwt = require('jsonwebtoken');
var variables = require('../config/variables');

securityRoutes.use(function (req, res, next) {
    var token = req.cookies.jwtToken;
    if (token) {
        jwt.verify(token, variables.jwtSecretToken, function (err, decoded) {
            if (err) {
                return res.redirect('/');
            } else {
                req.userInfo = decoded;
                next();
            }
        });
    } else {
        return res.redirect('/');
    }
});

module.exports = securityRoutes;
